package org.schoolnavigator.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.schoolnavigator.dao.EmployeeDAO;
import org.schoolnavigator.to.EmployeeTO;


public class EmployeeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -680308125021570530L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int page = 1;
		int recordsPerPage = 5;
		int noOfRecords = 0;
		int noOfPages = 0;
		RequestDispatcher reqDisp;
		System.out.println("===== inside EmployeeServlet ===== ");
		try {
			if(request.getParameter("page") != null){
				page = Integer.parseInt(request.getParameter("page"));
				EmployeeDAO empDAO = new EmployeeDAO();
				List<EmployeeTO> list = empDAO.viewAllEmp((page - 1) * recordsPerPage, recordsPerPage);
				noOfRecords = empDAO.getNoOfRecords();
				noOfPages = (int)Math.ceil(noOfRecords * 1.0 / recordsPerPage);
				System.out.println("list **************** " + list.size());
				request.setAttribute("employeeList", list);
				request.setAttribute("noOfPages", noOfPages);
				request.setAttribute("currentPage", page);
				
			}
			reqDisp = request.getRequestDispatcher("jsp/displayEmployee.jsp");
			reqDisp.forward(request, response);
			
		}catch(NullPointerException npe){
			npe.printStackTrace();
		}catch(SQLException sqle){
			sqle.printStackTrace();
		}
	}

}
